<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>


        var a = 1;
        function printA() {
            console.log(this)
            console.log(this.a);
        }
        var obj = {
            a: 2,
            foo: printA,
            bar: function () {
                printA();
            }
        }

        obj.foo();//2 
        obj.bar(); // 1


        var foo = obj.foo;
        window.foo(); //1
























        //this 进阶

        var liLei = {
            name: 'liLei',
            money: 10,
            buyPen: function () {
                this.money = this.money - 1;
                console.log(this.name + " have money:" + this.money)
            }
        }

        var hanMeiMei = {
            name: 'hanMeiMei',
            money: 20,
            buyPan: function () {
                this.money = this.money - 2;
                console.log(this.name + " have money:" + this.money)
            }
        }

        // liLei.buyPen();

        // hanMeiMei.buyPan();

        liLei.buyPen.apply(hanMeiMei)
        hanMeiMei.buyPan.apply(liLei)
    </script>
</body>

</html>